<%@page import="Model.ListWord"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="html" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<core:view>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>eWiki</title>
            <link rel="stylesheet" media="screen" type="text/css" title="Design" href="../design.css"/>
              <script language="Javascript">
                var isMozilla = (navigator.userAgent.toLowerCase().indexOf('gecko')!=-1) ? true : false;
                var regexp = new RegExp("[\r]","gi");

                function storeCaret(selec,d)
                {
                        if (isMozilla)
                        {
                        // Si on est sur Mozilla
                                oField = document.forms['news'].elements['text'];
                                objectValue = oField.value;
                                deb = oField.selectionStart;
                                fin = oField.selectionEnd;
                                objectValueDeb = objectValue.substring( 0 , oField.selectionStart );
                                objectValueFin = objectValue.substring( oField.selectionEnd , oField.textLength );
                                objectSelected = objectValue.substring( oField.selectionStart ,oField.selectionEnd );
                        //	alert("Debut:'"+objectValueDeb+"' ("+deb+")\nFin:'"+objectValueFin+"' ("+fin+")\n\nSelectionné:'"+objectSelected+"'("+(fin-deb)+")");
                                if(d==2){
                                    oField.value = objectValueDeb + selec  + objectSelected + selec + objectValueFin;
                                    oField.selectionStart = strlen(objectValueDeb);
                                    oField.selectionEnd = strlen(objectValueDeb + selec + objectSelected + selec);
                                } else if (d==1){
                                    oField.value = objectValueDeb + selec  + objectSelected + objectValueFin;
                                    oField.selectionStart = strlen(objectValueDeb);
                                    oField.selectionEnd = strlen(objectValueDeb + selec + objectSelected );
                                } else if (d==3){
                                    oField.value = objectValueDeb + selec  + objectSelected + "]" + objectValueFin;
                                    oField.selectionStart = strlen(objectValueDeb);
                                    oField.selectionEnd = strlen(objectValueDeb + selec + objectSelected + "]");
                                }
                                oField.focus();
                                oField.setSelectionRange(
                                    objectValueDeb.length + selec.length + 2,
                                    objectValueDeb.length + selec.length + 2);
                        }
                        else
                        {
                        // Si on est sur IE
                                oField = document.forms['news'].elements['text'];
                                var str = document.selection.createRange().text;

                                if (str.length>0)
                                {
                                // Si on a selectionné du texte
                                        var sel = document.selection.createRange();
                                        if (d==1)
                                            sel.text = selec + str ;
                                        else if (d==2)
                                            sel.text = selec + str + selec ;
                                        else if (d==3)
                                            sel.text = selec + str + "]" ;
                                        sel.collapse();
                                        sel.select();
                                }
                                else
                                {
                                        oField.focus(oField.caretPos);
                                        oField.focus(oField.value.length);
                                        oField.caretPos = document.selection.createRange().duplicate();
                                        var bidon = "%~%";
                                        var orig = oField.value;
                                        oField.caretPos.text = bidon;
                                        var i = oField.value.search(bidon);
                                        if (d==1)
                                            oField.value = orig.substr(0,i) + selec + orig.substr(i, oField.value.length);
                                        else if (d==2)
                                            oField.value = orig.substr(0,i) + selec + selec + orig.substr(i, oField.value.length);
                                        else if (d==3)
                                            oField.value = orig.substr(0,i) + selec + "]" + orig.substr(i, oField.value.length);
                                        var r = 0;
                                        for(n = 0; n < i; n++)
                                        {if(regexp.test(oField.value.substr(n,2)) == true){r++;}};
                                        pos = i + 2 + selec.length - r;
                                        var r = oField.createTextRange();
                                        r.moveStart('character', pos);
                                        r.collapse();
                                        r.select();
                                }
                        }
                }/* http://www.editeurjavascript.com/countus/ */
        </script>
    <script src="passparam.js"></script>
    <script LANGUAGE="JavaScript">
	function Envoyer(f) {
			f.submit();
	}
    </script>

    </head>
        <body>
            <div class="pageTitre">
                eWiki - <span id="session"><html:outputText value="#{listWord.word}" /></span><span id="in">(edit)</span>
            </div>
            <form class="cadreMenu" id="Top">
                <a href="<%=request.getContextPath()%>/wiki/" class="bouton">Home</a> |
                <a href="<%=request.getContextPath()%>/wiki/<html:outputText value="#{listWord.word}" />!view" class="bouton">View</a> |
                <a href="<%=request.getContextPath()%>/wiki/<html:outputText value="#{listWord.word}" />!hist" class="bouton">History</a>
            </form>
            <form method="POST" name="news" action="<%=request.getContextPath()%>/wiki/<html:outputText value="#{listWord.word}" />" id="page">
                <% if (request.getAttribute("msg") != null) { %>
                    <div id="msg"><%=request.getAttribute("msg") %></div>
                <% } %>
                <input type="button" value="B" style="width:50px;font-weight:bold" onclick="storeCaret('++',2)">
                <input type="button" value="I" style="width:50px;font-style:italic" onclick="storeCaret('+',2)">
                <input type="button" value="[lien]" style="width:50px;text-decoration:underline" onclick="storeCaret('[',3)">
                <input type="button" value="http://"style="width:50px" onclick="storeCaret('http://',1)">
                <input type="button" value="<h1>"style="width:50px" onclick="storeCaret('! ',1)">
                <input type="button" value="<h2>"style="width:50px" onclick="storeCaret('!! ',1)">
                <input type="button" value="<h3>"style="width:50px" onclick="storeCaret('!!! ',1)">
                <input type="button" value="ligne"style="width:50px" onclick="storeCaret('---',1)"><br>

                    <input type="hidden" name="lastVersion" value="<html:outputText value="#{listWord.myLastVersion}" />" />
                    <input type="hidden" name="version" value="<html:outputText value="#{listWord.selectVersion}" />" />
                    
                    <textarea name="text" id="content" rows="25" wrap="virtual" cols="45" onkeyup="document.getElementById('mon_div').innerHTML=this.value"><html:outputText value="#{listWord.text}" /></textarea>
                    <input type="submit" value="valide" name="save" />
                <div id="Preview">
                    <!-- Ici doit etre affichier la preview! -->
                    Prévisualisation
                    <textarea id="mon_div"  readonly="true" rows="25" cols="45" onkeyup="<html:outputText value="#{listWord.words}" />"></textarea>
                </div>
                <br />Informations : <input type="text" name="info" id="infoVers" value="" />
                <input type="hidden" name="lastVersion" value="<html:outputText value="#{listWord.myLastVersion}" />" />
                <input type="hidden" name="selectversion" value="<html:outputText value="#{listWord.selectVersion}" />" />
                <input type="hidden" name="save" value="ok" />
                <input type="button" value="valide" name="btmsave" onclick="if (getElementById('infoVers').value == '') alert('Indiquer une information.'); else this.form.submit();" />
            </form>
            <div class="cadreMenu" id="bottom">
                <div id="r">
                    Kilian Panot &amp; Matthieu Barile
                </div>
                <div id="l">
                    <a href="<%=request.getContextPath()%>/wiki/!list" class="bouton">Liste des mots</a>
                </div>
            </div>
        </body>
    </html>
</core:view>